package com.systemCapacity.service.impl;

import com.systemCapacity.common.dto.BattleCapacityDto;
import com.systemCapacity.common.dto.EquipmentTemplateDto;
import com.systemCapacity.common.dto.EvaluateDto;
import com.systemCapacity.entity.BattlePlatformDevice;
import com.systemCapacity.entity.Enum.CapacityCoverage;
import com.systemCapacity.entity.Enum.PlatformType;
import com.systemCapacity.entity.Enum.TemplateType;
import com.systemCapacity.entity.Equipment;
import com.systemCapacity.entity.EquipmentTemplate;
import com.systemCapacity.service.*;
import net.sf.saxon.serialize.TEXTEmitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SystemCapacityEvaluateServiceImpl implements SystemCapacityEvaluateService {

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    BattleCapacityService battleCapacityService;

    @Autowired
    ActivityResourceService activityResourceService;

    @Autowired
    EquipmentTemplateService equipmentTemplateService;

    @Autowired
    BattlePlatformDeviceService battlePlatformDeviceService;

    static final int CHAIN_SIZE = 7;

    static final int PLATFORM_SIZE = 5;

    @Override
    public List<EvaluateDto> getEvaluate(int capacityType) {
        List<EquipmentTemplateDto> equipmentTemplateDtoList = equipmentTemplateService.getRedPlatfomList();
        List<BattleCapacityDto> capacities = battleCapacityService.getBattleCapacities(capacityType, "");

        List<EvaluateDto> dtos = new ArrayList<>();
        for (BattleCapacityDto capacity : capacities) {
            EvaluateDto dto = new EvaluateDto();
            dto.setCapacityId(capacity.getCapacityId());
            dto.setCapacityName(capacity.getCapacityName());
            dto.setChildDtos(new ArrayList<>());
            if (capacity.getChildren() == null || capacity.getChildren().isEmpty()) {
                continue;
            }
            for (BattleCapacityDto childDto : capacity.getChildren()) {
                EvaluateDto evaluateDto = new EvaluateDto();
                evaluateDto.setCapacityId(childDto.getCapacityId());
                evaluateDto.setCapacityName(childDto.getCapacityName());
                evaluateDto.setRelatedTypes(childDto.getRelatedTypes());
                //初始化平台能力数据
                Map<String,CapacityCoverage> coverageMap = new HashMap<>();
                for (EquipmentTemplateDto equipmentTemplateDto : equipmentTemplateDtoList) {
                    coverageMap.put(equipmentTemplateDto.getTemplateName(),CapacityCoverage.UNINVOLVED);
                }
                evaluateDto.setCoverageMap(coverageMap);
                List<BattlePlatformDevice> battlePlatformDeviceList = battlePlatformDeviceService.queryPlatformDeviceByCapacityId(childDto.getCapacityId());
                List<String> equipmentIds = battlePlatformDeviceList.stream().map(BattlePlatformDevice::getEquipmentId).collect(Collectors.toList());
                List<String> templateIds = equipmentService.getEquipmentTemplates(equipmentIds);
                List<EquipmentTemplate> templates;
                if (templateIds.isEmpty()) {
                    templates = new ArrayList<>();
                } else {
                    templates = equipmentTemplateService.getBaseMapper().selectBatchIds(templateIds);
                }
                List<Equipment> equipments;
                if (templateIds.isEmpty()) {
                    equipments = new ArrayList<>();
                } else {
                    equipments = equipmentService.getBaseMapper().selectBatchIds(equipmentIds);
                }
//                evaluateChain(evaluateDto, childDto, equipments, templates);
                evaluatePlatform(evaluateDto, childDto, equipments, templates);
//                evaluateCapacity(evaluateDto, childDto);
                dto.getChildDtos().add(evaluateDto);
            }
            dtos.add(dto);
        }
        return dtos;
    }

    /**
     * 评价作战能力
     * @param evaluateDto 作战评价
     * @param capacityDto 作战能力
     */
    private void evaluateCapacity(EvaluateDto evaluateDto, BattleCapacityDto capacityDto) {
        List<String> equipmentIds = activityResourceService.getRelatedEquipmentId(capacityDto.getCapacityId());
        List<String> templateIds = equipmentService.getEquipmentTemplates(equipmentIds);
        List<Integer> types = equipmentTemplateService.getTemplateType(templateIds);
        // 判断types是否覆盖指定能力的需求
        StringBuilder capacity = new StringBuilder();
        capacityDto.getRelatedType().forEach(type -> capacity.append(type.getValue()).append(";"));
        StringBuilder real = new StringBuilder();
        types.forEach(type -> real.append(type).append(";"));
        for (TemplateType type : TemplateType.values()) {
            if (capacity.toString().contains(String.valueOf(type.getValue()))) {
                if (real.toString().contains(String.valueOf(type.getValue()))) {
                    evaluateDto.getSatisfied().add(CapacityCoverage.COVERED);
                } else {
                    evaluateDto.getSatisfied().add(CapacityCoverage.UNCOVER);
                }
            } else if (real.toString().contains(String.valueOf(type.getValue()))) {
                evaluateDto.getSatisfied().add(CapacityCoverage.REDUNDANT);
            } else {
                evaluateDto.getSatisfied().add(CapacityCoverage.UNINVOLVED);
            }
        }
    }

    /**
     * 评价平台需求
     * @param evaluateDto 作战评价
     * @param capacityDto 作战能力
     * @param equipments 作战能力关联装备
     * @param templates 作战装备模板
     */
    private void evaluatePlatform(EvaluateDto evaluateDto, BattleCapacityDto capacityDto, List<Equipment> equipments,
                                  List<EquipmentTemplate> templates) {

        for (String relatedType : evaluateDto.getRelatedTypes()) {
            evaluateDto.getCoverageMap().put(relatedType,CapacityCoverage.UNCOVER);
        }
        for (EquipmentTemplate template : templates) {
            if(evaluateDto.getCoverageMap().get(template.getTemplateName()).equals(CapacityCoverage.UNCOVER)){
                evaluateDto.getCoverageMap().put(template.getTemplateName(),CapacityCoverage.COVERED);
            }
        }
    }

    /**
     * 评价数据链需求
     * @param evaluateDto 作战评价
     * @param capacityDto 作战能力
     * @param equipments 作战能力关联装备
     * @param templates 作战装备模板
     */
    private void evaluateChain(EvaluateDto evaluateDto, BattleCapacityDto capacityDto, List<Equipment> equipments,
                               List<EquipmentTemplate> templates) {
        List<CapacityCoverage> coverages = new ArrayList<>();
        List<String> types = capacityDto.getRelatedTypes();
        for (int i = 0; i < CHAIN_SIZE; i++) {
            if (types.get(i) == "1") {
                coverages.add(CapacityCoverage.UNCOVER);
            } else {
                coverages.add(CapacityCoverage.UNINVOLVED);
            }
        }

        for (Equipment equipment : equipments) {
            switch (equipment.getEquipmentName()) {
                case "JIDS链":
                    if (types.get(0) == "1" && (coverages.get(0).getValue().intValue() != CapacityCoverage.COVERED.getValue().intValue())) {
                        coverages.set(0, CapacityCoverage.COVERED);
                    }
                    break;
                case "综合数据链":
                    if (types.get(1) == "1" && (coverages.get(1).getValue().intValue() != CapacityCoverage.COVERED.getValue().intValue())) {
                        coverages.set(1, CapacityCoverage.COVERED);
                    }
                    break;
                case "通用情报宽带机间数据":
                    if (types.get(2) == "1" && (coverages.get(2).getValue().intValue() != CapacityCoverage.COVERED.getValue().intValue())) {
                        coverages.set(2, CapacityCoverage.COVERED);
                    }
                    break;
                case "机间数据链":
                    if (types.get(3) == "1" && (coverages.get(3).getValue().intValue() != CapacityCoverage.COVERED.getValue().intValue())) {
                        coverages.set(3, CapacityCoverage.COVERED);
                    }
                    break;
                case "通用导弹数据链":
                    if (types.get(4) == "1" && (coverages.get(4).getValue().intValue() != CapacityCoverage.COVERED.getValue().intValue())) {
                        coverages.set(4, CapacityCoverage.COVERED);
                    }
                    break;
                case "航空武器协同全向数据链":
                    if (types.get(5) == "1" && (coverages.get(5).getValue().intValue() != CapacityCoverage.COVERED.getValue().intValue())) {
                        coverages.set(5, CapacityCoverage.COVERED);
                    }
                    break;
                case "海上编队宽带协同数据链":
                    if (types.get(6) == "1" && (coverages.get(6).getValue().intValue() != CapacityCoverage.COVERED.getValue().intValue())) {
                        coverages.set(6, CapacityCoverage.COVERED);
                    }
                    break;
            }
        }
        evaluateDto.setSatisfied(coverages);
    }
}
